package com.longcai.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author zh
 * @ClassName cn.aduu.util.Generator
 * @Description
 * @date 2018-02-22 11:37:14
 */
public class Generator {

    private static final String dbUrl = "jdbc:mysql://47.105.145.14:3306/chenglianyijia_db?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false";

    private static final String driver = "com.mysql.jdbc.Driver";

    private static final String username = "root";

    private static final String password = "123456";

    private static final String author = "f";

    public static void main(String[] args) {
        String packageName = "com.longcai";
        //auth -> UserService, 设置成true: auth -> IUserService
        boolean serviceNameStartWithI = false;
        generateByTables(serviceNameStartWithI, packageName, author, "clyj_fuyou_city_num");

        System.out.println("completed...");
    }

    /**
     * @param serviceNameStartWithI
     * @param packageName           包名
     * @param author                作者
     * @param tableNames            表名
     */
    private static void generateByTables(boolean serviceNameStartWithI, String packageName, String author, String... tableNames) {
        // 1. 全局配置
        GlobalConfig config = new GlobalConfig();
        // 2.数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        // 设置数据库类型
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername(username)
                .setPassword(password)
                .setDriverName(driver);
        // 3.策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                // 全局大写命名
                .setCapitalMode(true)
                // 启用lombok
                .setEntityLombokModel(true)
                // 指定表名 字段名是否启用下划线
                .setDbColumnUnderline(true)
                // 数据库表映射到实体的命名策略
                .setNaming(NamingStrategy.underline_to_camel)
                // .setSuperMapperClass("cn.saytime.mapper.BaseMapper")
                // .setTablePrefix()
                // 修改替换成你需要的表名，多个表名传数组
                .setInclude(tableNames);
        config.setActiveRecord(false)
                .setAuthor(author)
                // 生成文件指定路径
                .setOutputDir("d:\\clyjq")
                // 文件覆盖
                .setFileOverride(true)
                // 生成基本的ResultMap
                .setBaseResultMap(true)
                // 生成基本的sql片段
                .setBaseColumnList(true)
                .setEnableCache(false);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                                .setMapper("mapper")
                                .setService("service")
                                .setServiceImpl("service.impl")
                                .setXml("mybatis.mappers")
                ).execute();
    }
}
